const Joi = require('joi');

async function userInfoValidate(params) {
  const schema = Joi.object({
    id: Joi.string().required().messages({
      'string.empty': '用户id不能为空',
      'any.required': '用户id是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function userListValidate(params) {
  const schema = Joi.object({
    name: Joi.string(),
    login_name: Joi.string(),
    sex: Joi.string(),
    method: Joi.string().default('desc'),
    order: Joi.string().default('create_time'),
    pageIndex: Joi.string().default(1),
    pageSize: Joi.string().default(10),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function userInsertValidate(params) {

  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '用户名称不能为空',
        'any.required': '用户名称是必填项',
        'string.pattern': '名称中不能包含特殊字符',
      }),
    login_name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '登录名不能为空',
        'any.required': '登录名是必填项',
        'string.pattern': '登录名中不能包含特殊字符',
      }),
    sex: Joi.number().required().messages({
      'number.empty': '性别不能为空',
      'any.required': '性别是必填项',
    }),
    password: Joi.string()
      .required()
      .min(8)
      .pattern(/^(?=.*[A-Z])(?=.*[!@#$%^&*]).+$/)
      .messages({
        'string.empty': '密码不能为空',
        'any.required': '密码是必填项',
        'string.pattern': '密码中必须至少包含一个大写字母一个小写字母一个特殊字符',
        'string.min': '密码不能少于8位',
      }),
    phone: Joi.string()
      .pattern(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)
      .messages({
        'string.pattern': '非法电话号码',
      }),
    email: Joi.string().email().messages({
      'string.email': '非法邮箱',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function userUpdateValidate(params) {
  const schema = Joi.object({
    id: Joi.string()
      .required()
      .messages({
        'string.empty': '用户id不能为空',
        'any.required': '用户id是必填项',
      }),
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.pattern': '名称中不能包含特殊字符',
      }),
    sex: Joi.number(),
    phone: Joi.string()
      .pattern(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)
      .messages({
        'string.pattern': '非法电话号码',
      }),
    email: Joi.string().email().messages({
      'string.email': '非法邮箱',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function userDeleteValidate(params) {
  const schema = Joi.object({
    ids: Joi.array().items(Joi.string()),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function register(params) {
  const schema = Joi.object({
    name: Joi.string()
      .required()
      .messages({
        'string.empty': '用户名称不能为空',
        'any.required': '用户名称是必填项',
      }),

    login_name: Joi.string()
      .required()
      .messages({
        'string.empty': '登录名称不能为空',
        'any.required': '登录名称是必填项',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function roleListValidate(params) {
  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '角色名称不能为空',
        'any.required': '角色名称是必填项',
        'string.pattern': '角色名称中不能包含特殊字符',
      }),
    order: Joi.string().default('create_time'),
    method: Joi.string().default('desc'),
    pageIndex: Joi.string().default('1'),
    pageSize: Joi.string().default('10'),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function roleInsertValidate(params) {
  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '角色名称不能为空',
        'any.required': '角色名称是必填项',
        'string.pattern': '角色名称中不能包含特殊字符',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function roleUpdateValidate(params) {
  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '角色名称不能为空',
        'any.required': '角色名称是必填项',
        'string.pattern': '角色名称中不能包含特殊字符',
      }),
    uuid: Joi.string()
      .required()
      .messages({
        'string.empty': '角色id不能为空',
        'any.required': '角色uuid是必填项',
      }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function roleDeleteValidate(params) {
  const schema = Joi.object({
    name: Joi.array(),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }

  async;
}

async function permissionListValidate(params) {
  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '角色名称不能为空',
        'any.required': '角色名称是必填项',
        'string.pattern': '角色名称中不能包含特殊字符',
      }),
    key: Joi.string().required().messages({
      'string.empty': 'key不能重复',
      'any.required': 'key是必填项',
    }),
    url: Joi.string().required().messages({
      'string.empty': '地址不能为空',
      'any.required': '地址是必填项',
    }),
    order: Joi.string().default('create_time'),
    method: Joi.string().default('desc'),
    pageIndex: Joi.string().default('1'),
    pageSize: Joi.string().default('10'),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function permissionInsertValidate(params) {
  const schema = Joi.object({
    name: Joi.string()
      .pattern(/^[^\s]+$/)
      .required()
      .messages({
        'string.empty': '名称不能为空',
        'any.required': '名称是必填项',
        'string.pattern': '名称中不能包含特殊字符',
      }),
    key: Joi.string().required().messages({
      'string.empty': 'key不能为空',
      'any.required': 'key是必填项',
    }),
    url: Joi.string().required().messages({
      'string.empty': 'url不能为空',
      'any.required': 'url是必填项',
    }),
    parent_id: Joi.string().required().default('0')
      .messages({
        'any.required': 'parent_id为必填项',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function permissionUpdateValidate(params) {

  const schema = Joi.object({
    uuid: Joi.string().required().messages({
      'string.empty': 'uuid不能为空',
      'any.required': 'uuid是必填项',
    }),
    name: Joi.string()
      .pattern(/^[^\s]+$/)
      .required()
      .messages({
        'string.empty': '名称不能为空',
        'any.required': '名称是必填项',
        'string.pattern': '名称中不能包含特殊字符',
      }),
    key: Joi.string().required().messages({
      'string.empty': 'key不能为空',
      'any.required': 'key是必填项',
    }),
    url: Joi.string().required().messages({
      'string.empty': 'url不能为空',
      'any.required': 'url是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function permissionDeleteValidate(params) {

  const schema = Joi.object({
    ids: Joi.array().required().messages({
      'array.empty': 'ids不能为空',
      'any.required': 'ids是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function addressListValidate(params) {

  const schema = Joi.object({
    pageIndex: Joi.string().default(1),
    pageSize: Joi.string().default('10'),
    order: Joi.string().default('create_time'),
    method: Joi.string().default('create_time'),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function addressInsertValidate(params) {
  const schema = Joi.object({
    address: Joi.string().required().messages({
      'string.empty': '地址不能为空',
      'any.required': '地址是必填项',
    }),
    user_email: Joi.string().email().messages({
      'string.email': '邮箱格式错误',
    }),
    user_phone: Joi.string()
      .pattern(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)
      .messages({
        'string.pattern': '非法电话号码',
      }),
    user_name: Joi.string()
      .required()
      .messages({
        'string.empty': '名称不能为空',
        'any.required': '名称是必填项',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function addressUpdateValidate(params) {

  const schema = Joi.object({
    address: Joi.string().required().messages({
      'string.empty': '地址不能为空',
      'any.required': '地址是必填项',
    }),
    user_email: Joi.string().email().messages({
      'string.email': '邮箱格式错误',
    }),
    user_phone: Joi.string()
      .pattern(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)
      .messages({
        'string.pattern': '非法电话号码',
      }),
    user_name: Joi.string()
      .required()
      .messages({
        'string.empty': '名称不能为空',
        'any.required': '名称是必填项',
      }),
    uuid: Joi.string().required().messages({
      'string.empty': 'uuid不能为空',
      'any.required': 'uuid为必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function addressDeleteValidate(params) {
  const schema = Joi.object({
    ids: Joi.array(),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function createUserAndRoleValidate(params) {
  const schema = Joi.object({
    user_id: Joi.string().required().messages({
      'string.empty': '用户id不能为空',
      'any.required': '用户id为必填项',
    }),
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function updateUserAndRoleValidate(params) {
  const schema = Joi.object({
    user_id: Joi.string().required().messages({
      'string.empty': '用户id不能为空',
      'any.required': '用户id为必填项',
    }),
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function deleteUserAndRoleValidate(params) {
  const schema = Joi.object({
    user_id: Joi.string().required().messages({
      'string.empty': '用户id不能为空',
      'any.required': '用户id为必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function getRoleAndPermissionValidate(params) {
  const schema = Joi.object({
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function createRoleAndPermissionValidate(params) {
  const schema = Joi.object({
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
    permission_id: Joi.array().required().messages({
      'array.empty': '权限id不能为空',
      'any.required': '权限id是必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function updateRoleAndPermissionValidate(params) {
  const schema = Joi.object({
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
    permission_id: Joi.array().required().messages({
      'array.empty': '权限id不能为空',
      'any.required': '权限id是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


async function deleteRoleAndPermissionValidate(params) {

  const schema = Joi.object({
    role_id: Joi.string().required().messages({
      'string.empty': '角色id不能为空',
      'any.required': '角色id为必填项',
    }),
    permission_id: Joi.array().required().messages({
      'array.empty': '权限id不能为空',
      'any.required': '权限id是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }

}

async function forgetPasswordValidate(params) {

  const schema = Joi.object({
    newPassword: Joi.string()
      .required()
      .min(8)
      .pattern(/^(?=.*[A-Z])(?=.*[!@#$%^&*]).+$/)
      .messages({
        'string.empty': '密码不能为空',
        'any.required': '密码是必填项',
        'string.pattern': '密码中必须至少包含一个大写字母一个小写字母一个特殊字符',
        'string.min': '密码不能少于8位',
      }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw error;
  }
}

async function changePasswordValidate(params) {
  const schema = Joi.object({
    newPassword: Joi.string()
      .required()
      .min(8)
      .pattern(/^(?=.*[A-Z])(?=.*[!@#$%^&*]).+$/)
      .messages({
        'string.empty': '密码不能为空',
        'any.required': '密码是必填项',
        'string.pattern': '密码中必须至少包含一个大写字母一个小写字母一个特殊字符',
        'string.min': '密码不能少于8位',
      }),

    user_id: Joi.string().required().messages({
      'string.empty': '用户id不能为空',
      'any.required': '用户id是必填项',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

module.exports = {
  userInfoValidate,
  userListValidate,
  userInsertValidate,
  userUpdateValidate,
  userDeleteValidate,
  roleListValidate,
  roleInsertValidate,
  roleUpdateValidate,
  roleDeleteValidate,
  register,
  permissionListValidate,
  permissionInsertValidate,
  permissionUpdateValidate,
  permissionDeleteValidate,
  addressListValidate,
  addressInsertValidate,
  addressUpdateValidate,
  addressDeleteValidate,
  createUserAndRoleValidate,
  updateUserAndRoleValidate,
  deleteUserAndRoleValidate,
  getRoleAndPermissionValidate,
  createRoleAndPermissionValidate,
  updateRoleAndPermissionValidate,
  deleteRoleAndPermissionValidate,
  forgetPasswordValidate,
  changePasswordValidate,
};
